package com.topnav.mc.schedule.job;

import com.js.lib.cache.Cache;
import com.sun.management.OperatingSystemMXBean;
import com.topnav.mc.system.CpuUsedBean;
import com.topnav.mc.system.MySystemInfo;
import com.topnav.mc.system.ServerInfo;
import com.topnav.mc.system.SystemInfoUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;

import javax.annotation.Resource;
import java.io.File;
import java.io.Serializable;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 系统监控
 * @Author: kenny
 * @Date: 2023/10/9 16:09
 */
@Component
@Slf4j
public class SystemInfoMonitorJob implements Job, Serializable {
    @Resource(name="localCache")
    private Cache cache;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            MySystemInfo mySystemInfo = (MySystemInfo)cache.get("mySystemInfo");
            if (mySystemInfo == null){
                mySystemInfo = new MySystemInfo();
                cache.set("mySystemInfo",mySystemInfo);
            }

            ServerInfo serverInfo = (ServerInfo)cache.get("serverInfo");
            if (serverInfo == null){
                serverInfo = new ServerInfo();
                cache.set("serverInfo",serverInfo);
            }

            //内存
            double memoUsedRate = SystemInfoUtils.getMemInfo();
            mySystemInfo.setUsesdMemoryRate(memoUsedRate);
            mySystemInfo.setFreeMemoryRate(1.0-memoUsedRate);

            //CPU
            double cpuUsedRate = SystemInfoUtils.getCpuInfo();
            mySystemInfo.setCpuUsedRate(cpuUsedRate);
            mySystemInfo.setCpuIdleRate(1.0-cpuUsedRate);

            if(mySystemInfo.getCpuBean().size() > 10){
                mySystemInfo.getCpuBean().remove(0);
            }

            //double cupUsedRate = Math.round(cpuUsedRate*10000)/100.0;
            //double cpuIdleRate = Math.round((1.0-cpuUsedRate)*10000)/100.0;

            mySystemInfo.getCpuBean().add(new CpuUsedBean( Math.round(cpuUsedRate*10000)/100.0,Math.round((1.0-cpuUsedRate)*10000)/100.0));

/*
            //SystemInfo systemInfo = new SystemInfo();
            OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
            MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
            // 椎内存使用情况
            MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
            // 操作系统
            serverInfo.setOsName(System.getProperty("os.name"));
            mySystemInfo.setTotalMemorySize(osmxb.getTotalPhysicalMemorySize() / 1024.0 / 1024 / 1024);
            mySystemInfo.setFreeMemorySize(osmxb.getFreePhysicalMemorySize() / 1024.0 / 1024 / 1024);
            mySystemInfo.setUsesdMemorySize((osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / 1024.0 / 1024 / 1024);
            serverInfo.setTotalMemorySize(new DecimalFormat("#.##").format(mySystemInfo.getTotalMemorySize())+"G");
            serverInfo.setFreeMemorySize(new DecimalFormat("#.##").format(mySystemInfo.getFreeMemorySize())+"G");
            serverInfo.setUsesdMemorySize(new DecimalFormat("#.##").format(mySystemInfo.getUsesdMemorySize())+"G");

            // 获得线程总数
            ThreadGroup parentThread;
            for (parentThread = Thread.currentThread().getThreadGroup(); parentThread.getParent() != null; parentThread = parentThread.getParent()) {
            }
            int totalThread = parentThread.activeCount();
            serverInfo.setTotalThread(String.valueOf(totalThread));

            CentralProcessor processor = systemInfo.getHardware().getProcessor();
            long[] prevTicks = processor.getSystemCpuLoadTicks();
            TimeUnit.SECONDS.sleep(1);
            long[] ticks = processor.getSystemCpuLoadTicks();
            long nice = ticks[CentralProcessor.TickType.NICE.getIndex()]
                    - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
            long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()]
                    - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
            long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()]
                    - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
            long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()]
                    - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
            long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()]
                    - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
            long user = ticks[CentralProcessor.TickType.USER.getIndex()]
                    - prevTicks[CentralProcessor.TickType.USER.getIndex()];
            long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()]
                    - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
            long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()]
                    - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
            long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;

            serverInfo.setCpuProcessorCount(String.valueOf(processor.getLogicalProcessorCount()));
            serverInfo.setCpuIdleRate(new DecimalFormat("#.##%").format(idle * 1.0 / totalCpu));
            serverInfo.setCpuUsedRate(new DecimalFormat("#.##%").format((cSys + user) * 1.0 / totalCpu));

            double cupUsedRate = Math.round((cSys + user) * 1.0 / totalCpu*10000)/100.0;
            double cpuIdleRate = 100-mySystemInfo.getCpuUsedRate();

            mySystemInfo.setCpuUsedRate(cupUsedRate);
            mySystemInfo.setCpuIdleRate(cpuIdleRate);

            if(mySystemInfo.getCpuBean().size() > 10){
                mySystemInfo.getCpuBean().remove(0);
            }
            mySystemInfo.getCpuBean().add(new CpuUsedBean(cupUsedRate,cpuIdleRate));*/

        }catch (Exception e) {
            //e.printStackTrace();
            log.error(e.toString());
        }





    }
}
